import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
import 'package:scoped_model/scoped_model.dart';
import 'package:sudoku/page/bootstrap.dart';
import 'package:sudoku/page/sudoku_game.dart';
import 'package:sudoku/state/sudoku_state.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  SystemChrome.setPreferredOrientations(
    [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown],
  );
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  Future<SudokuState> _loadState() async {
    SudokuState sudokuState = await SudokuState.resumeFromDB();
    return sudokuState;
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder<SudokuState>(
      future: _loadState(),
      builder: (context, AsyncSnapshot<SudokuState> snapshot) {
        if (snapshot.connectionState == ConnectionState.waiting) {
          return Container(
            color: Colors.white,
            alignment: Alignment.center,
            child: Center(
              child: Text(
                '数独加载中...',
                style: TextStyle(color: Colors.black),
                textDirection: TextDirection.ltr,
              ),
            ),
          );
        }
        SudokuState sudokuState = snapshot.data;
        BootstrapPage bootstrapPage = BootstrapPage();
        SudokuGamePage sudokuGamePage = SudokuGamePage(title: "数独游戏");
        return ScopedModel(
          model: sudokuState,
          child: MaterialApp(
            title: '数独游戏',
            home: bootstrapPage,
            routes: <String, WidgetBuilder>{
              "/bootstrap": (context) => bootstrapPage,
              "/newGame": (context) => SudokuGamePage(title: "数独游戏"),
              "/gaming": (context) => sudokuGamePage
            },
          ),
        );
      },
    );
  }
}
